package com.problem.leetcode;

/**
 * Merge two sorted linked lists and return it as a new list.
 * The new list should be made by splicing together the nodes of the first two lists.
 *
 * @author yanzhuoleo
 * @version V1.0
 * @Description: Problem21
 * @date 2018-01-05-下午7:47
 */

public class Problem21 {

    public ListNode mergeTwoLists(ListNode l1, ListNode l2) {
        ListNode newListNode = new ListNode(-1);
        ListNode currentNode = newListNode;

        while (l2 != null && l1 != null) {
            if(l1.val >= l2.val) {
                currentNode.next = new ListNode(l2.val);
                l2 = l2.next;
            }else {
                currentNode.next = new ListNode(l1.val);
                l1 = l1.next;
            }
            currentNode = currentNode.next;
        }

        ListNode tmp = null;
        if(l2 != null) {
            tmp = l2;
        }

        if(l1 != null) {
            tmp = l1;
        }

        if(tmp != null) {
            while (tmp != null) {
                currentNode.next = new ListNode(tmp.val);
                currentNode = currentNode.next;
                tmp = tmp.next;
            }
        }
       return newListNode.next;
    }

    public class ListNode {
        int      val;
        ListNode next;

        ListNode(int x) { val = x; }
    }

}
